<?php
namespace App\Services;

use App\Repositories\Goods\CategoryModel;
use App\Repositories\Index\BannerModel;

class IndexService
{
    private static $bannerModel;
    private static $categoryModel;
    /**
     * 依赖注入
     * @param BannerModel $bannerModel
     * @param CategoryModel $categoryModel
     */
    public function __construct(
        BannerModel $bannerModel,
        CategoryModel $categoryModel
    )
    {
        self::$bannerModel = $bannerModel;
        self::$categoryModel = $categoryModel;
    }

    /**
     * 获取轮播图地址
     * User: zhouyao
     * Date: 2018/8/26
     * Time: 下午3:39
     * @param $params
     * @return array|int|mixed
     * @throws \App\Exceptions\AppException
     */
    public function bannerList($params)
    {
        if (empty($params) || !isset($params['pid'])) {
            app_fail(9997);
        }
        // 获取轮播图
        $where = [
        ['pid', '=', $params['pid']],
        ['start_time', '<=', time()],
        ['end_time', '>=', time()],
    ];
        $select = ['ad_name', 'ad_link', 'ad_code'];
        $result = self::$bannerModel->getAllData($where, $select, 'orderby', 'asc');

        return $result->isEmpty() ? [] : $result;
    }

    /**
     * 获取商品分类
     * User: zhouyao
     * Date: 2018/8/26
     * Time: 下午5:28
     * @param $params
     * @return array|mixed
     * @throws \App\Exceptions\AppException
     */
    public function category($params)
    {
        if (empty($params) || !isset($params['parent_id'])) {
            app_fail(9997);
        }

        $where = [
            'is_show' => 1,
            'parent_id' => $params['parent_id']
        ];
        $result = self::$categoryModel->getAllData($where);

        return $result->isEmpty() ? [] : $result;
    }
}